/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 张宇田
 * Date: 2025-08-29
 * Time: 15:27
 */
public class MyLinkedList {

    static class Node {
        int val;
        Node prev;
        Node next;

        public Node(int val) {
            this.val = val;
        }
    }

    public Node head;
    public Node last;

    public void addFirst(int val) {
        Node node = new Node(val);

        if (head == null) {
            head = last = node;

        } else {
            node.next = head;
            head.prev = node;
            head = node;
            head.prev = null;
        }
    }

    public void addLast(int val) {
        Node node = new Node(val);

        if (head == null) {
            head = last = node;

        } else {
            node.prev = last;
            last.next = node;
            last = node;
            last.next = null;
        }

    }

    public void addIndex(int index, int val) {
        if (index < 0 && index > size()) {
            return;
        }
        if (head == null) {
            return;
        }
        //然后要让 cur走到index处
        Node cur = findIndex(index);
        Node node = new Node(val);

        node.next = cur;
        node.prev = cur.prev;
        cur.prev = node;
        node.prev.next = node;


    }

    private Node findIndex(int index) {
        Node cur = head;
        int count = 0;
        while (count != index) {
            cur = cur.next;
            count++;
        }

        return cur;
    }

    public void remove(int key){
        if (head == null) {
            return;
        }

        Node cur = head;
        while(cur!=null){
            if(cur.val == key){
                if (cur == head){
                    if (cur.next == null){
                        head = null;
                        last = null;
                    }else{
                        head = head.next;
                        head.prev = null;
                    }

                }else{
                    if (cur == last){
                        last = last.prev;
                        last.next = null;
                    }else{
                        cur.prev.next = cur.next;
                        cur.next.prev = cur.prev;
                    }
                }
                return;
            }else {
                cur = cur.next;
            }
        }
    }

    public void removeAll(int key){
        if (head == null) {
            return;
        }

        Node cur = head;
        while(cur!=null){
            if(cur.val == key){
                if (cur == head){
                    if (cur.next == null){
                        head = null;
                        last = null;
                    }else{
                        head = head.next;
                        head.prev = null;
                    }

                }else{
                    if (cur == last){
                        last = last.prev;
                        last.next = null;
                    }else{
                        cur.prev.next = cur.next;
                        cur.next.prev = cur.prev;
                    }
                }
                cur = cur.next;
            }else {
                cur = cur.next;
            }
        }
    }

    public void show() {
        if (head == null) {
            return;
        }
        Node cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
    }

    public int size() {
        int count = 0;
        Node cur = head;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

}
